// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
var db = null;
angular.module('starter', ['ionic', 'ngCordova','starter.controllers', 'starter.controllers2', 'starter.services'])

.run(function($ionicLoading, $ionicPlatform, $cordovaSQLite, SOAP, DB) {

  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    // if (window.cordova && window.cordova.plugins.Keyboard) {
    //   cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    // }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }

    if (ionic.Platform.platform() == 'ios' ||
        ionic.Platform.platform() == 'android') {
          // db = sqlitePlugin.openDatabase({name: "ttexam.db", createFromLocation: 1});
          // $ionicLoading.show({template: "加载中..."})
          // SOAP.LoadRemoteBaseItemChangeList(function(basechangeres){
          //   $ionicLoading.hide()
          //   var items = JSON.parse(basechangeres.getLoadRemoteBaseItemChangeListResult())
          //   $.each(items, function(i, v){
          //       $cordovaSQLite.execute(db, "select * from sys_serverexchangeitem where id = ?", [v.id]).then(
          //         function(selectres){
          //           if (selectres.rows.length == 1){
          //             dbdate = new Date(selectres.rows.item(0).dupdatedate)
          //             remotedate = new Date(v.dupdatedate)
          //             if (remotedate > dbdate) {
          //               console.log("New Update " + JSON.stringify(selectres.rows.item(0)))
          //               if (selectres.rows.item(0).citemname == 'course'){
          //                 SOAP.LoadBaseItemUpdateNewData(function(itemsoapres){
          //                   DB.updateCourse(JSON.parse(itemsoapres.getLoadBaseItemUpdateNewDataResult()))
          //                 },outputerr, 'course', '')
          //               } else if(selectres.rows.item(0).citemname == 'courseclass'){
          //                 SOAP.LoadBaseItemUpdateNewData(function(itemsoapres){
          //                   DB.updateCourseClass(JSON.parse(itemsoapres.getLoadBaseItemUpdateNewDataResult()))
          //                 },outputerr, 'courseclass', '')
          //               } else if (selectres.rows.item(0).citemname == 'coursesubclass'){
          //                 SOAP.LoadBaseItemUpdateNewData(function(itemsoapres){
          //                   DB.updateCourseSubclass(JSON.parse(itemsoapres.getLoadBaseItemUpdateNewDataResult()))
          //                 },outputerr, 'coursesubclass', '')  
          //               }
          //               DB.updateSysExchangeItem(v);
          //             }
          //           } else {
          //             console.log("no such item " + JSON.stringify(v))
          //           }
          //         },outputerr
          //         )
          //     })
          // },function(err){$ionicLoading.hide(); $ionicPopup.alert({title: "加载失败,请检查网络"})})
    }
  });
})

.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {


  $stateProvider

  .state('app', {
    url: "/app",
    abstract: true,
    templateUrl: "templates/menu.html",
    controller: 'AppCtrl'
  })

  .state('app.classes', {
    url: "/classes", 
    views: {
      'tab-courses': {
        templateUrl: 'templates/classes.html',
        controller: 'ClassesCtrl'
      }
    }
  })
  .state('app.class', {
    url: '/classes/:classid',
    views: {
      'tab-courses': {
        templateUrl: 'templates/class.html',
        controller: 'ClassCtrl'
      }
    }
  })
  .state('app.courses',{
    url: '/courses/:subclassid',
    views: {
      'tab-courses': {
        templateUrl: 'templates/courses.html',
        controller: 'CoursesCtrl'
      }
    }
  })
  .state('app.home',{
    url: '/home/:currentCourseId',
    views: {
      'tab-home': {
        templateUrl: 'templates/home.html',
        controller: 'HomeCtrl'
      }
    }
  })
  .state('correct',{
    url: '/correct/:courseId',
    templateUrl: 'templates/correct.html',
    controller: 'CorrectCtrl'
  })
  .state('correctlist',{
    url: '/correctlist/:chapterId',
    templateUrl: 'templates/correctlist.html',
    controller: 'CorrectListCtrl'
  })
  .state('collect',{
    url: '/collect/:courseId',
    templateUrl: 'templates/correct.html',
    controller: 'CollectCtrl'
  })
  .state('collectlist',{
    url: '/collectlist/:chapterId',
    templateUrl: 'templates/correctlist.html',
    controller: 'CollectListCtrl'
  })
  .state('choose_chapter',{
    url: '/choose_chapter/:courseId',
    templateUrl: 'templates/choose_chapter.html',
    controller: "ChooseChapterCtrl"
  })
  .state('choose_subchapter',{
    url: '/choose_subchapter/:courseId/:chapterId',
    templateUrl: 'templates/choose_subchapter.html',
    controller: 'ChooseSubChapterCtrl'
  })
  .state('practice',{
    url: '/practice/:chapterId/:correct/:collect/:indexId',
    templateUrl: 'templates/practice.html',
    controller: 'PracticeCtrl'
  })
  .state('correctsubject',{
    url: '/correctsubject',
    templateUrl: 'templates/correctsubject.html',
    controller: "PracticeCtrl"
  })
  .state('usernote',{
    url: '/usernote',
    templateUrl: 'templates/usernote.html',
    controller: 'PracticeCtrl'
  })
  .state("simulation",{
    url: '/simulation/:courseId',
    templateUrl: 'templates/simulation.html',
    controller: 'SimCtrl'
  })
  .state("create_simulation",{
    url: '/create_simulation/:courseId',
    templateUrl: 'templates/create_simulation.html',
    controller: 'CreateSimCtrl'
  })
  .state("do_simulation", {
    url: '/do_simulation/:paperId',
    templateUrl: 'templates/do_simulation.html',
    controller: "DoSimCtrl"
  })
  .state("login", {
    url: '/login',
    templateUrl: 'templates/login.html',
    controller: "LoginCtrl"
  })
  .state("regist", {
    url: '/regist',
    templateUrl: 'templates/regist.html',
    controller: "RegistCtrl"
  })
  .state("findpass",{
    url: '/findpass',
    templateUrl: 'templates/findpass.html',
    controller: "FindPassCtrl"
  })
  .state('app.active',{
    url: '/active',
    views: {
      'tab-home' :{
        templateUrl: 'templates/active.html',
        controller: 'HomeCtrl'
      }
    }
  })
  .state("app.cloud", {
    url: '/cloud',
    views: {
      'tab-home' : {
        templateUrl: 'templates/cloud.html',
        controller: 'HomeCtrl'
      }
    }
  })
  .state("app.setting",{
    url: '/setting',
    views: {
      'tab-info' : {
        templateUrl: 'templates/setting.html',
        controller: 'SettingCtrl'
      }
    }
  })
  .state("app.selectfont",{
    url: '/selectfont',
    views: {
      'tab-info' : {
        templateUrl: 'templates/select_font.html',
        controller: 'SettingCtrl'
      }
    }
  })
  .state("app.searchcourse",{
    url: '/searchcourse',
    views:  {
      'tab-info' : {
        templateUrl: 'templates/searchcourse.html',
        controller: 'SearchCtrl'
      }
    }
  })  
  .state("app.info", {
    url: '/info/:cusId',
    views: {
      'tab-info' : {
        templateUrl: 'templates/info.html',
        controller: 'InfoCtrl'
      }
    }
  })
  ;
  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/login');
})
.config(function($ionicConfigProvider){
  $ionicConfigProvider.views.maxCache(1);
  $ionicConfigProvider.views.forwardCache(false);
  $ionicConfigProvider.backButton.text('返回').icon('ion-chevron-left');
  $ionicConfigProvider.tabs.style("standard")
})
